/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package huang.fb;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;

/**
 *
 * @author huang
 */
public class AutoCompleteServlet extends HttpServlet {
    
    private ServletContext context;
    private BusStationData bsData = new BusStationData();
    private HashMap busStations = bsData.getBusStation();
    
    @Override
    public void init(ServletConfig config) throws ServletException {
        this.context = config.getServletContext();
    }
    
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException {
        
        
   
        
       

        String action = request.getParameter("action");
        String targetId = request.getParameter("id");
        StringBuffer sb = new StringBuffer();

        if (targetId != null) {
            targetId = targetId.trim().toLowerCase();
        } else {
            context.getRequestDispatcher("/error.jsp").forward(request, response);
        }

        boolean namesAdded = false;
        if (action.equals("complete")) {

            // check if user sent empty string
            if (!targetId.equals("")) {

                Iterator it = busStations.keySet().iterator();

                while (it.hasNext()) {
                    String id = (String) it.next();
                    BusStation busStation = (BusStation) busStations.get(id);

                    if ( // targetId matches name
                         busStation.getName().toLowerCase().startsWith(targetId)) {

                        sb.append("<busStation>");
                        sb.append("<id>").append(busStation.getID()).append("</id>");
                        sb.append("<name>").append(busStation.getName()).append("</name>");                        
                        sb.append("</busStation>");
                        namesAdded = true;
                    }
                }
            }

            if (namesAdded) {
                response.setContentType("text/xml");
                response.setHeader("Cache-Control", "no-cache");
                response.getWriter().write("<busStations>" + sb.toString() + "</busStations>");
            } else {
                //nothing to show
                response.setStatus(HttpServletResponse.SC_NO_CONTENT);
            }
        }

        if (action.equals("lookup")) {

            // put the target busStation in the request scope to display 
            if ((targetId != null) && busStations.containsKey(targetId.trim())) {
                request.setAttribute("complete-field", sb.toString());
                context.getRequestDispatcher("/index.jsp").forward(request, response);
            }
        }
    }



}
